#!/usr/bin/env php
<?php

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

echo "===========================================\n";
echo "  Telnet Server Starting...\n";
echo "===========================================\n";
echo "Server Host: 127.0.0.1\n";
echo "Server Port: 8765\n";
echo "Available Commands: mul, incr, div, conv_tree, help, exit\n";
echo "===========================================\n";
echo "Use: telnet 127.0.0.1 8765\n";
echo "Press Ctrl+C to stop the server\n";
echo "===========================================\n\n";

// 获取命令参数
$host = '127.0.0.1';
$port = 8765;

// 解析命令行参数
if ($argc > 1) {
    foreach ($argv as $index => $arg) {
        if ($arg === '--host' && isset($argv[$index + 1])) {
            $host = $argv[$index + 1];
        }
        if ($arg === '--port' && isset($argv[$index + 1])) {
            $port = $argv[$index + 1];
        }
        if ($arg === '--help' || $arg === '-h') {
            echo "Usage: php scripts/start-server.php [--host=127.0.0.1] [--port=8765]\n";
            exit(0);
        }
    }
}

try {
    $commandHandler = new App\Services\CommandHandler();
    $server = new App\Services\TelnetServer($commandHandler, $host, $port);
    $server->start();
} catch (Exception $e) {
    echo "Error starting server: " . $e->getMessage() . "\n";
    exit(1);
}